package com.example.demo.Controller;

import com.example.demo.Enity.User;
import com.example.demo.dao.ActivityJpa;
import com.example.demo.dao.UserJpa;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.*;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;


@RestController
@CrossOrigin(origins = "*", maxAge = 3600)
public class Login {

    @Autowired
    private UserJpa jpa;
    @Autowired
    private ActivityJpa activityJpa;

    //判断用户信息是否已经存储在数据库中
    @RequestMapping("/isExist")
    public String isExist(String yb_userid) {
        ArrayList<User> exist = jpa.isExist(yb_userid);
        if(exist.isEmpty()){
            return "true";
        }
        return "false";
    }

    @RequestMapping("/addUser")
    public  String addUser(String yb_schoolname, String yb_userhead, String yb_userid, String yb_usernick){
        System.out.println(yb_schoolname+yb_userhead+yb_userid+yb_usernick);
        jpa.addUser(yb_schoolname,  yb_userhead,  yb_userid,  yb_usernick);
        return  "ok";
    }

    //获取用户信息
    @RequestMapping("/getUserinfo")
    public Object getUserinfo(String access_token){
        System.out.println(access_token);
        RestTemplate restTemplate = new RestTemplate();
        Map<String, String> params = new HashMap<>();
        ResponseEntity<String> responseEntity = restTemplate.getForEntity("https://openapi.yiban.cn/user/me?access_token="+access_token,String.class, params);
        return responseEntity;
    }

    //判断用户是否过期
    @RequestMapping("/judgeAuto")
    public Object judgeAutoto(String access_token){
        System.out.println(access_token);
        RestTemplate restTemplate = new RestTemplate();
        MultiValueMap<String,String>params=new LinkedMultiValueMap<String, String>();
        params.add("client_id", "7f25df86b26a6536");
        params.add("access_token",access_token);
        HttpHeaders headers =new HttpHeaders();
        headers.setContentType(MediaType.MULTIPART_FORM_DATA);
        HttpEntity<String> request = new HttpEntity(params, headers);
        ResponseEntity<String> responseEntity = restTemplate.postForEntity("https://openapi.yiban.cn/oauth/token_info", request,String.class);
        return responseEntity;
    }
    @RequestMapping("/loginApp")
    public ArrayList loginApp(String username,String password){
        ArrayList<User> a=jpa.loginApp(username, password);
        return a;
    }
    @RequestMapping("/registerApp")
    public void registerApp(String username,String password,String nickName){
//        System.out.println(nickName+"nickname");
        jpa.registerApp(username, password,nickName,"http://img02.fs.yiban.cn/18414788/avatar/user/200");
    }
    @RequestMapping("/judgeRepeat")
    public boolean judgeRepeat(String username){
        ArrayList<User> a=jpa.findsum(username);
        System.out.println(username);
        System.out.println(a+""+a.size()+""+a.isEmpty());
        if(a.isEmpty()){
            return true;
        }
        return false;
    }
    @RequestMapping("/updateName")
    public String updateName(String username,String nickname){
        jpa.updateName(username, nickname);
        //并把所有林剑艺发布的活动名字全部换掉
        activityJpa.updateName(username, nickname);
        return nickname;
    }
    @RequestMapping("/updateImg")
    public String updateImg(String username,String img){
        jpa.updateImg(username, img);
        return "ok";
    }
}
